let u = navigator.userAgent.toLowerCase()
let chrome = u.match(/\bchrome\/(\d+)/)
let firefox = u.match(/\bfirefox\/(\d+)/)

const r = {
  agent: u,
  chrome: chrome && parseFloat(chrome[1]),
  firefox: firefox && parseFloat(firefox[1]),
  safari: !chrome && u.indexOf('safari') > 0,
  ie: (navigator.appName === 'Microsoft Internet Explorer' || u.indexOf('trident') > 0) && document.documentMode,
  compat: {
    gap: false
  },
}

if (u.match(/\bmobile\b/)) {
  r.mobile = true
  r.app = location.protocol === 'file:';
  let wechat = u.match(/(MicroMessenger)\/([\d\.]+)/i);
  if (wechat) r.wechat = {
    version: wechat[2].replace(/_/g, '.')
  };
  let android = u.match(/(Android);?[\s\/]+([\d.]+)?/i);
  if (android) {
    r.android = true;
    r.version = android[2];
    r.isBadAndroid = !(/Chrome\/\d/.test(window.navigator.appVersion))
  }
  let iphone = u.match(/(iPhone\sOS)\s([\d_]+)/i)
  if (iphone) {
    r.ios = r.iphone = true;
    r.version = iphone[2].replace(/_/g, '.')
  } else {
    let ipad = u.match(/(iPad).*OS\s([\d_]+)/i)
    if (ipad) {
      r.ios = r.ipad = true;
      r.version = ipad[2].replace(/_/g, '.')
    }
  }
}

if ((r.chrome && r.chrome < 84) || (r.firefox && r.firefox < 63)) {
  r.compat.gap = true
}

export default r
